import {Directive, ElementRef, EventEmitter, OnDestroy, Output, Renderer2} from '@angular/core';

@Directive({
  /*默认ID选择器*/
  selector: '[appPageBody]'
})
export class PageBodyDirective implements OnDestroy {
  mouseMouveListener: Function;

  @Output() mouseCloseToScreenLeft: EventEmitter<boolean> = new EventEmitter<boolean>();

  constructor(
    private el: ElementRef,
    private renderer2: Renderer2
  ) {
    this.mouseMouveListener = this.renderer2.listen(el.nativeElement, 'mousemove', (evt: MouseEvent) => {
      if (evt.screenX <= 10) {
        this.mouseCloseToScreenLeft.emit(true);
      }
      if (evt.screenX >= 220) {
        this.mouseCloseToScreenLeft.emit(false);
      }
    });
  }

  ngOnDestroy() {
    if (this.mouseMouveListener) {
      this.mouseMouveListener();
      this.mouseMouveListener = null;
    }
  }
}
